﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MechWars
{
    public class Platform
    {
        public static List<Polygon> polygon = new List<Polygon>();
        public Polygon activePoly = new Polygon();
        public static List<JMesh> mesh = new List<JMesh>();
        public JMesh activeMesh;        
        static Vector2[] inshials = { new Vector2(0, -1), new Vector2(-1f, 0), new Vector2(0, 1), new Vector2(0, 1), new Vector2(1f, 0f), new Vector2(0, -1) };

        public Vector3 position;
        int type = 0;
        public void Update()
        {
            activeMesh.Update();
            activeMesh.effect.World = Matrix.CreateTranslation(position);
            activePoly.origin.X = position.X;

        }
        public static void preCalculate()
        {
            polygon.Add(createPlafromPoly(150));
            mesh.Add(createPlafromMesh(150));
            for (int i = 1; i < 4; ++i)
            {
                polygon.Add(createPlafromPoly(i * 10));
                mesh.Add(createPlafromMesh(i * 10));
            }
        }
        public JMesh getMesh(JMesh i)
        {
            return i;
        }
        public static JMesh createPlafromMesh(float length)
        {
            Polygon returnPoly = new Polygon();

            float hleng = length * 0.5f;
            returnPoly.AddVertex(inshials[0] + new Vector2(-hleng, 0));
            returnPoly.AddVertex(inshials[1] + new Vector2(-hleng, 0));
            returnPoly.AddVertex(inshials[2] + new Vector2(-hleng, 0));

            returnPoly.AddVertex(inshials[3] + new Vector2(hleng, 0));
            returnPoly.AddVertex(inshials[4] + new Vector2(hleng, 0));
            returnPoly.AddVertex(inshials[5] + new Vector2(hleng, 0));

            JMesh returnMesh = new JMesh();
            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[0], 1), new Vector3(returnPoly.vertexs[1], 1), new Vector3(returnPoly.vertexs[1], -1), new Vector3(returnPoly.vertexs[0], -1));
            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[1], 1), new Vector3(returnPoly.vertexs[2], 1), new Vector3(returnPoly.vertexs[2], -1), new Vector3(returnPoly.vertexs[1], -1));
            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[2], 1), new Vector3(returnPoly.vertexs[3], 1), new Vector3(returnPoly.vertexs[3], -1), new Vector3(returnPoly.vertexs[2], -1));

            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[3], 1), new Vector3(returnPoly.vertexs[4], 1), new Vector3(returnPoly.vertexs[4], -1), new Vector3(returnPoly.vertexs[3], -1));
            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[4], 1), new Vector3(returnPoly.vertexs[5], 1), new Vector3(returnPoly.vertexs[5], -1), new Vector3(returnPoly.vertexs[4], -1));
            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[5], 1), new Vector3(returnPoly.vertexs[0], 1), new Vector3(returnPoly.vertexs[0], -1), new Vector3(returnPoly.vertexs[5], -1));

            returnMesh.AddTriangle(new Vector3(returnPoly.vertexs[0], -1), new Vector3(returnPoly.vertexs[1], -1), new Vector3(returnPoly.vertexs[2], -1));

            returnMesh.AddTriangle(new Vector3(returnPoly.vertexs[3], -1), new Vector3(returnPoly.vertexs[4], -1), new Vector3(returnPoly.vertexs[5], -1));

            returnMesh.AddQuad(new Vector3(returnPoly.vertexs[0], -1), new Vector3(returnPoly.vertexs[2], -1), new Vector3(returnPoly.vertexs[3], -1), new Vector3(returnPoly.vertexs[5], -1));

            return returnMesh;
        }
        public static Polygon createPlafromPoly(float length)
        {
            Polygon returnPoly = new Polygon();

            float hleng = length * 0.5f;
            returnPoly.AddVertex(inshials[0] + new Vector2(-hleng, 0));
            returnPoly.AddVertex(inshials[1] + new Vector2(-hleng, 0));
            returnPoly.AddVertex(inshials[2] + new Vector2(-hleng, 0));

            returnPoly.AddVertex(inshials[3] + new Vector2(hleng, 0));
            returnPoly.AddVertex(inshials[4] + new Vector2(hleng, 0));
            returnPoly.AddVertex(inshials[5] + new Vector2(hleng, 0));

            return returnPoly;
        }
        public Platform(Vector3 pos,int tp)
        {
            type = tp;
            activePoly.vertexs.AddRange(polygon[type].vertexs);
            activePoly.normals.AddRange(polygon[type].normals);
            activePoly.origin.X = pos.X;
            activePoly.origin.Y = pos.Y;

            activeMesh = new JMesh();
            activeMesh.effect.DiffuseColor = new Vector3(0, 0, 0);
            activeMesh.effect.View = mesh[type].effect.View;
            activeMesh.effect.Projection = mesh[type].effect.Projection;
            activeMesh.vertexes.AddRange( mesh[type].vertexes);
            activeMesh.effect.World = Matrix.CreateTranslation(position);
            position = pos;
        }
        public void Draw()
        {
            activeMesh.draw();
        }
    }
}
